package test.gdata.picasa;

import java.io.IOException;
import java.util.List;

import test.common.Settings;
import test.gdata.calendar.CalendarClient;
import test.gdata.docs.DocType;
import test.gdata.docs.DocumentClient;
import test.gdata.maps.GetMapsLinks;
import test.gdata.maps.ListMaps;
import test.gdata.maps.MapsClient;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.client.maps.MapsService;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.gtt.DocumentFeed;
import com.google.gdata.data.maps.MapEntry;
import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class Main {
	public static void main(String []args){
		System.out.println("Starting....");
//		try {
//			TestGetAlbum();
//			TestGetCalendarsEntrys();
			TestDocs();
//			args = new String[] {Settings.getApplicationSettings("username"), Settings.getApplicationSettings("password")};
//			GetMapsLinks.main(args);
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (ServiceException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		System.out.println("Ending....");
	}
	
	public static void TestGetAlbum() throws IOException, ServiceException
	{
		String appName = Settings.getApplicationSettings("applicationname");
		String username = Settings.getApplicationSettings("username");
		String password = Settings.getApplicationSettings("password");
		PicasawebClient client = new PicasawebClient(new PicasawebService(appName), username, password);
		List<AlbumEntry> albums = client.getAlbums();
		for (AlbumEntry albumEntry : albums) {
			System.out.println("Title: " + albumEntry.getTitle().getPlainText());
			System.out.println("Name: " + albumEntry.getName());
			System.out.println("# Photos: " + albumEntry.getPhotosUsed());
			
			List<PhotoEntry> photos = client.getPhotos(albumEntry);
			for (PhotoEntry photoEntry : photos) {
				System.out.println("Title: " + photoEntry.getTitle().getPlainText());
				System.out.println("Link.href: " + photoEntry.getHtmlLink().getHref());
				System.out.println("Link.krefLang: " + photoEntry.getHtmlLink().getHrefLang());
				System.out.println("Link.rel: " + photoEntry.getHtmlLink().getRel());
				List<MediaContent> mediaContents = photoEntry.getMediaContents();
				System.out.println("###### Media Content ######");
				for (MediaContent mediaContent : mediaContents) {
					System.out.println("Diemensions: " + mediaContent.getWidth() + " x " + mediaContent.getHeight());
					System.out.println("Url: " + mediaContent.getUrl());
				}
			}
			System.out.println();
			System.out.println();
		}
	}

	public static void TestGetCalendarsEntrys() throws IOException, ServiceException{
		String appName = Settings.getApplicationSettings("applicationname");
		String username = Settings.getApplicationSettings("username");
		String password = Settings.getApplicationSettings("password");
		CalendarClient client = new CalendarClient(new CalendarService(appName), username, password);
		List<CalendarEntry> calendars = client.getCalendarEntrys();
		for (CalendarEntry calendarEntry : calendars) {
			System.out.println("Calendar title: " + calendarEntry.getTitle().getPlainText());
			List<CalendarEventEntry> events = client.getCalendarEventEntrys(calendarEntry);
			System.out.println("############ Events ##############");
			for (CalendarEventEntry calendarEventEntry : events) {
				System.out.println("Event title: " + calendarEventEntry.getTitle().getPlainText());
				System.out.println("Event content: " + calendarEventEntry.getPlainTextContent());
				List<When> times = calendarEventEntry.getTimes();
				for (When when : times) {
					System.out.println("Start: " + when.getStartTime());
					System.out.println("End: " + when.getStartTime());
				}
			}
		}
	}
	
	public static void TestMaps() throws IOException, ServiceException{
		String appName = Settings.getApplicationSettings("applicationname");
		String username = Settings.getApplicationSettings("username");
		String password = Settings.getApplicationSettings("password");
		MapsClient client = new MapsClient(new MapsService(appName), username, password);
		client.info();
		List<MapEntry> maps = client.getMaps();
		for (MapEntry mapEntry : maps) {
			System.out.println("Map Title: " + mapEntry.getTitle().getPlainText()); 
			System.out.println("Map content: " + mapEntry.getPlainTextContent());
		}
	}

	public static void TestDocs()
	{
		String username = Settings.getApplicationSettings("username");
		String password = Settings.getApplicationSettings("password");
		try {
			DocumentClient client = new DocumentClient(username, password);
			DocumentListFeed docListFeed = client.getDocsListFeed(DocType.KML);
			if(docListFeed != null){
				for (DocumentListEntry doc : docListFeed.getEntries()) {
					System.out.println("Id: " + doc.getId());
					System.out.println("DocId: " + doc.getDocId());
					System.out.println("Filename: " + doc.getFilename());
					System.out.println("Type: " + doc.getType());
				}	
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
